<?php

namespace Web\MainSiteBundle\Controller;

use Model\SchoolBundle\Entity\School;
use Model\SchoolBundle\Model\SchoolModel;
use Model\TeacherBundle\Entity\Teacher;
use Model\TeacherBundle\Entity\TeacherApplySchool;
use Model\TeacherBundle\Model\TeacherApplySchoolModel;
use Model\TeacherBundle\Model\TeacherModel;
use Model\TeacherCategoryBundle\Entity\TeacherCategory;
use Model\TeacherCategoryBundle\Model\TeacherCategoryModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;

/**
 * @route("/Teacher/")
 */
class TeacherManageController extends WebBaseController
{
    /**
     * @route("edit",name="WebTeacherEdit")
     */
    public function WebTeacherEdit(Request $request)
    {
        $user = $this->needToBeTeacher();
        $userId = $user->getId();
        $teacherModel = new TeacherModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {
            if (!$category = $this->getParamRequired("category")) {return $this->js_out("请选择教师职称");}
            if (!$name = $this->getParamRequired("name")) {return $this->js_out("请填写教师称谓");}
            if (!$intro = $this->getParamRequired("intro")) {return $this->js_out("请填写教师简介");}

            $teacherInfo["category"] = $category;
            $teacherInfo["name"] = $name;
            $teacherInfo["intro"] = $intro;

            if ($id = $this->getParamRequired("id")) {

                $teacherModel->commUpdate($id,$teacherInfo);
            } else {
                $teacher = new Teacher();
                $em = $this->getDoctrine()->getManager();
                $teacher->setName($name);
                $teacher->setAddTime(time());
                $teacher->setCategory($category);
                $teacher->setIntro($intro);
                $teacher->setParams("[]");
                $teacher->setSchool(0);
                $teacher->setUserId($user->getId());
                $em->persist($teacher);
                $em->flush();
            }
        }

        //获取教师信息
        $teacherInfo = $teacherModel->getByUserId($userId);
        $data["teacher"] = $teacherInfo;
        //获取所有职称
        $categoryModel = new TeacherCategoryModel($this->getDoctrine());
        $data["category"] = $categoryModel->getAllTeacherCategory();
        return $this->render("WebMainSiteBundle:Teacher:info.html.twig",$data);
    }

    /**
     * @route("school",name="WebTeacherSchool")
     */
    public function WebTeacherSchool(Request $request)
    {
        $user = $this->needToBeTeacher();
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacherInfo = $teacherModel->getByUserId($user->getId());
        $schoolModel = new SchoolModel($this->getDoctrine());
        $schoolId = isset($teacherInfo["school"]) ? $teacherInfo["school"] : 0 ;
        $school = $schoolModel->getById($schoolId);

        if (!empty($school)) {//已加入教师
            $data["join"] = 1;
            $data["school"] = $school;
        } else {//尚未加入教师
            //分为：1.未申请任何教师；2.申请未通过的情况
            $teacherApplySchoolModel = new TeacherApplySchoolModel($this->getDoctrine());
            $apply = $teacherApplySchoolModel->getByTeacherId($teacherInfo["id"]);
            if (!empty($apply)) {
                $data["apply"] = $apply;
                $data["applySchool"] = $schoolModel->getById($apply["schoolId"]);
            }
            $data["join"] = 0;
            $schoolList = $schoolModel->getList();
            $data["schools"] = $schoolList;
        }
        return $this->render("WebMainSiteBundle:Teacher:school.html.twig",$data);
    }


    /**
     * @route("set/school/{id}",name="WebTeacherSetSchool")
     */
    public function WebTeacherSetSchool(School $school)
    {
        if (!$message = $this->getParamRequired("message")) {
            return $this->js_out("请填写留言信息。");
        }
        $user = $this->needToBeTeacher();
        $id = $school->getId();
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacher = $teacherModel->getByUserId($user->getId());
        $em = $this->getDoctrine()->getManager();
        $rep = $this->getDoctrine()->getRepository("ModelTeacherBundle:TeacherApplySchool");
        $chkApply = $rep->findOneBy(array("teacherId"=>$teacher["id"]));
        if ($chkApply) {
            $apply = $chkApply;
        } else {
            $apply = new TeacherApplySchool();
        }
        $apply->setAddTime(time());
        $apply->setTeacherId($teacher["id"]);
        $apply->setSchoolId($id);
        $apply->setStatus(0);
        $apply->setReply("");
        $apply->setMessage($message);
        $em->persist($apply);
        $em->flush();
        return $this->redirectToRoute("WebTeacherSchool");
    }

    /**
     * @route("del/apply/{id}",name="WebTeacherDelApply")
     */
    public function WebTeacherDelApply(TeacherApplySchool $teacherApplySchool)
    {
        $em = $this->getDoctrine()->getManager();
        $em->remove($teacherApplySchool);
        $em->flush();

        return $this->redirectToRoute("WebTeacherSchool");
    }


    /**
     * @route("leave",name="WebLeaveSchool")
     */
    public function WebSchoolLeave(Request $request)
    {
        return $this->msg_out("警告：确定要与学校解除合作关系吗？",$this->generateUrl("WebDoLeaveSchool"));
    }

    /**
     * @route("do/leave",name="WebDoLeaveSchool")
     */
    public function WebSchoolDoLeave(Request $request)
    {
        $user = $this->needToBeTeacher();
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacher = $teacherModel->getByUserId($user->getId());
        $teacherModel->commUpdate($teacher["id"],array("school"=>0));
        return $this->redirectToRoute("WebUserIndex");
    }

    /**
     * @route("home",name="WebTeacherHome")
     */
    public function WebTeacherHome(Request $request)
    {
        $user = $this->needToBeTeacher();
        $teacherModel = new TeacherModel($this->getDoctrine());
        $teacher = $teacherModel->getByUserId($user->getId());

        return $this->redirectToRoute("WebUserIndex");
    }

}
